/**
 * meilele.com Inc.
 * Copyright (c) 2004-2016 All Rights Reserved.
 */
package com.gpagers.ui;

import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.gpagers.core.ObjectRef;

/**
 * 
 * @author liuzijie@meilele.com
 * @version $Id: TopDg.java, v 0.1 2016年5月11日 下午4:29:16 liuzijie@meilele.com Exp $
 */
public class TopDg extends Dialog {

    protected Object result;
    protected Shell  shell;
    private String   msg;
    private static final int width = 450;
    private static final int height = 182;
    private AudioClip ac;
    /**
     * Create the dialog.
     * @param parent
     * @param style
     */
    public TopDg(Shell parent, int style, String msg) {
        super(parent, style);
        setText("消息");
        this.msg = msg;
        try {
            URL pathBase1 = Main.class.getResource("../../../");
            URL pathBase = new URL(pathBase1+ObjectRef.voicePath);
            System.out.println(pathBase.getPath());
            ac = Applet.newAudioClip(new URL(pathBase+ObjectRef.voicePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the dialog.
     * @return the result
     */
    public Object open() {
        createContents();
        shell.open();
        shell.layout();
        Display display = getParent().getDisplay();
        Rectangle bounds = display.getPrimaryMonitor().getBounds();
        Rectangle rect = shell.getBounds();
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        shell.setLocation(x, y);
        ac.play();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    public void close() {
        ac.stop();
        shell.close();
        shell.dispose();
       
    }

    public boolean isDisposed() {
        return shell.isDisposed();
    }

    /**
     * Create contents of the dialog.
     */
    private void createContents() {
        shell = new Shell(getParent(), getStyle());
        shell.setSize(width, height);
        shell.setText(getText());
        //窗口最前
        OS.SetWindowPos(shell.handle, OS.HWND_TOPMOST, 0, 0, width, height, SWT.NULL);

        Label lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setBounds(10, 10, 128, 128);
        lblNewLabel
            .setImage(new Image(ObjectRef.display, ObjectRef.imagePath + "images/console.png"));

        Label lblNewLabel_1 = new Label(shell, SWT.NONE);
        lblNewLabel_1.setFont(SWTResourceManager.getFont("微软雅黑", 16, SWT.NORMAL));
        lblNewLabel_1.setBounds(171, 45, 238, 46);
        lblNewLabel_1.setText(msg);
    }

    public Shell getShell() {
        return shell;
    }

    public void setShell(Shell shell) {
        this.shell = shell;
    }

    public Display getDisplay() {
        return getParent().getDisplay();
    }

}
